<#assign ctx=request.contextPath />

<!-- toolbar-->
<div id="applications_tb" style="padding:5px;height:auto">
    <div style="margin-bottom:5px">
        <a href="#" class="easyui-linkbutton" iconCls="icon-plus" plain="true"
           id="applications_add">添加</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true"
           id="applications_edit">修改</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true"
           id="applications_delete">删除</a>
    </div>
</div>

<!--datagrid-->
<table class="easyui-datagrid" id="applications_datagrid" toolbar="#applications_tb"
       data-options="url:'${ctx}/applications/datagrid.json',rownumbers:true,singleSelect:true,fitColumns:true,method:'get',striped:true,pagination:true,pageSize:20,pageList:[20,30,40,50,100]">
    <thead>
    <tr>
        <th field="name" width="10%">应用名称</th>
        <th field="appId" width="25%">AppId</th>
        <th field="appKey" width="25%">AppKey</th>
        <th field="created" width="20%">创建时间</th>
        <th field="updated" width="20%">更新时间</th>
    </tr>
    </thead>
</table>


<div id="applications_win_add" class="easyui-window"
     data-options="closed:true,title:'应用',modal:true,collapsible:false,minimizable:false,maximizable:false"
     style="width:300px;height:200px;padding:5px;">
    <form id="applications_form_add" method="post">
        <div class="form-control">
            <label for="name">应用名称:</label>
            <input class="easyui-validatebox" type="text" name="name" required="required"/>
        </div>
        <div class="form-control" style="text-align: center;">
            <a id="applications_add_save" href="#" class="easyui-linkbutton"
               data-options="iconCls:'icon-save'">保存</a>
        </div>
    </form>
</div>

<div id="applications_win_edit" class="easyui-window"
     data-options="closed:true,title:'应用',modal:true,collapsible:false,minimizable:false,maximizable:false"
     style="width:500px;height:300px;padding:5px;">
    <form id="applications_form_edit" method="patch">
        <div class="form-control">
            <label for="name">应用名称:</label>
            <input class="easyui-validatebox" type="text" name="name" readonly="readonly"/>
        </div>
        <div class="form-control">
            <label for="name">AppId:</label>
            <input class="easyui-validatebox" type="text" name="appId" readonly="readonly"/>
        </div>
        <div class="form-control">
            <label for="name">AppKey:</label>
            <input class="easyui-validatebox" type="text" name="appKey" readonly="readonly"
                   style="width: 50%"/>
            <a id="applications_edit_refresh" href="#" class="easyui-linkbutton"
               data-options="iconCls:'icon-refresh'">重置</a>
        </div>
    </form>
</div>

<script type="text/javascript">
    //系统配置
    $("#applications_add").click(function () {
        $("#applications_win_add").window('open');
    });

    $("#applications_edit").click(function () {

        var selection = $("#applications_datagrid").datagrid('getSelected');
        if (null == selection) {
            $.messager.show({
                                msg: "请选择要编辑的数据",
                                title: "确认"
                            });
            return;
        }

        $("#applications_form_edit").form('load', '${ctx}/applications/' + selection.id + ".json");
        $("#applications_win_edit").window('open');

    });

    $("#applications_delete").click(function () {

        var selection = $("#applications_datagrid").datagrid('getSelected');

        if (null == selection) {
            $.messager.show({
                                msg: "请选择要删除的数据",
                                title: "确认"
                            });

            return;
        }

        core.func.del('${ctx}/applications/' + selection.id + '.json', function (data) {
            if (data.status == 0) {
                $.messager.show({
                                    msg: "删除成功",
                                    title: "提示"
                                });
                $("#applications_datagrid").datagrid('reload');
            } else {
                $.messager.show({
                                    msg: "删除失败",
                                    title: "失败"
                                });
            }
        });

    });

    $("#applications_add_save").click(function () {

        if ($("#applications_form_add").form('validate')) {

            var params = $("#applications_form_add").serialize();

            core.func.post('${ctx}/applications.json', params, function (data) {
                if (data.status == 0) {
                    $("#applications_form_add").form('clear');
                    $("#applications_win_add").window('close');
                    $("#applications_datagrid").datagrid('load');
                    $.messager.show({
                                        msg: "应用保存成功",
                                        title: "成功",
                                        timeout: 1000
                                    });
                } else if (data.status == -1) {
                    $.messager.show({
                                        msg: "应用名称已经存在",
                                        title: "失败"
                                    });
                } else {
                    $.messager.show({
                                        msg: "应用保存失败",
                                        title: "失败"
                                    });
                }
            });

        }

    });

    $("#applications_edit_refresh").click(function () {

        var selection = $("#applications_datagrid").datagrid('getSelected');

        $.messager.confirm("确认", "重置后其他使用该Key的系统将不能正常访问,确定要重置该AppKey吗？", function (r) {
            if (r) {
                core.func.patch('${ctx}/applications/' + selection.id + '/tokens.json', null,
                                function (data) {
                                    if (data.status == 0) {
                                        $("#applications_form_edit").form('clear');
                                        $("#applications_win_edit").window('close');
                                        $("#applications_datagrid").datagrid('reload');
                                        $.messager.show({
                                                            msg: "appKey重置成功",
                                                            title: "成功",
                                                            timeout: 1000
                                                        });
                                    } else if (data.status == -1) {
                                        $.messager.show({
                                                            msg: "应用不存在",
                                                            title: "失败"
                                                        });
                                    } else {
                                        $.messager.show({
                                                            msg: "appKey重置失败",
                                                            title: "失败"
                                                        });
                                    }
                                });
            }
        });

    });

</script>